package com.satchi.struts.action.useraction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.satchi.hib.pojo.FinRole;
import com.satchi.hib.pojo.FinUser;
import com.satchi.service.UserAddService;
import com.satchi.struts.form.UserAddForm;

public class UseraddAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserAddForm uaf = (UserAddForm) form;
		UserAddService serv = new UserAddService();
		int id = uaf.getUid();
		FinUser fu = null;
		if (id < 0) {
			fu = new FinUser();
			fu.setUname(uaf.getUname());
			fu.setUpwd(uaf.getUpwd());
		} else {
			fu = serv.get(FinUser.class, id);
			String pass = uaf.getUpwd();
			if (pass != null && !pass.equals(""))
				fu.setUpwd(uaf.getUpwd());
		}
		FinRole fr = new FinRole();
		fr.setRoleId(uaf.getRoleId());
		fu.setRole(fr);
		fu.setDepartment(uaf.getDepartment());
		request.setAttribute("fu", fu);
		serv.saveOrupdate(fu);
		return mapping.findForward("success");
	}

}
